package com.whenjun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.kq.KqLocationGroupDoorAddReq;
import com.whenjun.dto.kq.KqLocationGroupDoorDTO;
import com.whenjun.dto.kq.KqLocationGroupDoorReq;
import com.whenjun.entity.IscDoor;
import com.whenjun.entity.KqLocationGroupDoor;
import com.whenjun.mapper.KqLocationGroupDoorMapper;
import com.whenjun.service.IKqLocationGroupDoorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-22
 */
@Service
public class KqLocationGroupDoorServiceImpl extends ServiceImpl<KqLocationGroupDoorMapper, KqLocationGroupDoor> implements IKqLocationGroupDoorService {

    @Autowired
    private IscDoorServiceImpl iscDoorService;

    @Transactional
    public void KqLocationGroupDoorAdd(KqLocationGroupDoorAddReq kqLocationGroupDoorAddReq) {
        for (String indexCode: kqLocationGroupDoorAddReq.getIndexCodes() ) {
            // 是否已加入
            KqLocationGroupDoor kqLocationGroupDoor = this.lambdaQuery()
                    .eq(KqLocationGroupDoor::getLocationGroupId, kqLocationGroupDoorAddReq.getLocationGroupId())
                    .eq(KqLocationGroupDoor::getIndexCode, indexCode)
                    .eq(KqLocationGroupDoor::getState, false)
                    .one();


            if (kqLocationGroupDoor != null) {
//                throw new FootBookException("该门禁点已加入该分组");
                continue;
            }

            LocalDateTime now = LocalDateTime.now();
            String userId = SaTokenUtil.getUserId();

            kqLocationGroupDoor = new KqLocationGroupDoor();
            kqLocationGroupDoor.setLocationGroupId(kqLocationGroupDoorAddReq.getLocationGroupId());
            kqLocationGroupDoor.setIndexCode(indexCode);
            kqLocationGroupDoor.setCreateTime(now);
            kqLocationGroupDoor.setCreateUser(userId);
            kqLocationGroupDoor.setState(false);

            this.save(kqLocationGroupDoor);
        }
    }

    @Transactional
    public void KqLocationGroupDoorRm(KqLocationGroupDoorAddReq kqLocationGroupDoorAddReq) {
        for (String indexCode: kqLocationGroupDoorAddReq.getIndexCodes() ) {
            // 是否已加入
            KqLocationGroupDoor kqLocationGroupDoor = this.lambdaQuery()
                    .eq(KqLocationGroupDoor::getLocationGroupId, kqLocationGroupDoorAddReq.getLocationGroupId())
                    .eq(KqLocationGroupDoor::getIndexCode, indexCode)
                    .eq(KqLocationGroupDoor::getState, false)
                    .one();


            if (kqLocationGroupDoor == null) {
//                throw new FootBookException("该门禁点未加入该分组");
                continue;
            }

            LocalDateTime now = LocalDateTime.now();
            String userId = SaTokenUtil.getUserId();

            kqLocationGroupDoor.setUpdateTime(now);
            kqLocationGroupDoor.setUpdateUser(userId);
            kqLocationGroupDoor.setState(true);

//            this.updateById(kqLocationGroupDoor);
            this.removeById(kqLocationGroupDoor);
        }
    }

    @Transactional
    public void KqLocationGroupDoorRmByLocationGroupId(String locationGroupId) {
        LambdaQueryWrapper<KqLocationGroupDoor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KqLocationGroupDoor::getLocationGroupId, locationGroupId);
        this.remove(queryWrapper);
    }

    @Transactional
    public List<KqLocationGroupDoorDTO> KqLocationGroupDoors(KqLocationGroupDoorReq kqLocationGroupDoorReq) {
//        MPJLambdaWrapper<IscDoor> wrapper = new MPJLambdaWrapper<>();
//        wrapper.selectAll(IscDoor.class)
//                .leftJoin(KqLocationGroupDoor.class, KqLocationGroupDoor::getIndexCode, IscDoor::getIndexCode)
//                .eq(KqLocationGroupDoor::getLocationGroupId, kqLocationGroupDoorReq.getLocationGroupId())
//                .eq(KqLocationGroupDoor::getState, false)
//                .orderByDesc(KqLocationGroupDoor::getCreateTime);

        List<KqLocationGroupDoorDTO> kqLocationGroupDoorDTOS = iscDoorService.getBaseMapper().selectByLocationId(kqLocationGroupDoorReq.getLocationGroupId());

        return kqLocationGroupDoorDTOS;
    }
}
